<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

// 玩家 根据游戏角色  新增 流失用户统计 只与昨日对比
class CdkeyRef extends  Model{

	protected $table='game_cdkey_ref';
    //protected $_db_select = 'mysql';

    // public $timestamps = null;
    public static $status_arr = [0 => 'valid cdkey', 1 => 'running', 2=> 'used'];

    const DB_FIELDS = 'id,patch_id,patch_id,status,player_id,account,cdkey';
    
	public function paginate()
    {
    	$perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $rw = \DB::connection($this->selectdb())->table($this->table);
        self::SourceCondition($rw);
        $total = $rw->get()->count();
        $rw->orderBy("id", 'desc')->skip($start)->take($perPage);
        $data = $rw->get();
        $data = $data->toArray();

        //$total = \DB::connection($this->selectdb())->table($this->table)->get()->count();

        //$data = json_decode($data, true);
        // var_dump($data);
        // exit;

        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
    public function getKeyName()
    {
        return "patch_id";
    }

    public function selectdb()
    {
        return 'mysql_bgac';
    }
    public function getConnectionName(){
        return $this->selectdb();
    }
    public function getAreaListAttribute($value)
    {
        return explode(',', $value);
    }

    public function setAreaListAttribute($value)
    {
        $this->attributes['area_list'] = implode(',', $value);
    }

    public static function SourceCondition(&$rw)
    {
        $cond = Request::all();
        if ($cond['title']){
            $rw->where("title", 'like', $cond["title"].'%');
        }
        if ($cond['area_list']){
            $rw->where("area_list", 'like', $cond['area_list']);
        }

        $rw->where('patch_id', $cond['patch_id']?:0);
        // if (isset($cond["expired_at"])){
        //     $send_time = $cond['expired_at'];
        //     (0 == $send_time) && $rw->where('expired_at', 0);
        // }
    }
    

    public static function assignByOut(&$one, $out){
        $flds = explode(',', self::DB_FIELDS);
        
 
        foreach ($flds as $fld) {
            isset($out[$fld]) && $one->{$fld} = $out[$fld];
        }

    }
    
    public static function GetCodeCount($patch_id = 0)
    {
        $rw = \DB::connection('mysql_bgac')->table('game_cdkey_ref');
        $rw->where('patch_id', $patch_id);
        $count = $rw->get()->count();
        return $count ?:0;
    }

        // 实现自定义导出数据源
    public static function getCustomExportData()
    {
        $cp_area = Request::get('cp_area');
        $tableAccount = 'game_cdkey_ref';
        $rw = \DB::connection('mysql_bgac')->table($tableAccount);
        self::SourceCondition($rw, $cp_area);
        $rw->orderBy('id', "DESC");
        $total = $rw->get()->count();
        $data = $rw->get()->toArray();
        
        $export_data = [];
        foreach ($data as $obj) {
            $tmp_arr = [];
            foreach(self::getCustomExportHead() as $attr => $value){
                $tmp_arr[$attr] = $obj->{$attr} ?:' ';
            }

            $export_data[] = $tmp_arr;
        }
        \Log::debug(__METHOD__, ['custom define success!', 'export count'=> $total]);
        return $export_data;
    }

    public static function getCustomExportHead()
    {
        return [
            'cdkey'         =>  'CdKey',
            'account'       =>  'Account',
            'player_id'     =>  'Player ID',
            'patch_id'      =>  'CP Proo ID',
            'status'        =>  'Prop Name',
        ];
    }
}